package io.renren.common.utils.PayUtils.WxPay.weixin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.renren.common.utils.PayUtils.WxPay.weixin.util.ConstantUtil;
import io.renren.common.utils.PayUtils.WxPay.weixin.util.MD5Util;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class ClientRequestHandler extends PrepayIdRequestHandler {

	public ClientRequestHandler(HttpServletRequest request,
			HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	public String getXmlBody() {
		StringBuffer sb = new StringBuffer();
		Set es = super.getAllParameters().entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			if (!"appkey".equals(k)) {
				sb.append("<" + k + ">" + v + "<" + k + ">" + "\r\n");
			}
		}
		return sb.toString();
	}
	
	/**
	 * 创建签名MD5
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws Exception
	 */
	public String createMD5Sign() throws UnsupportedEncodingException {
		StringBuffer sb = new StringBuffer();
		Set<?> es = super.getAllParameters().entrySet();
		Iterator<?> it = es.iterator();
		while (it.hasNext()) {
			@SuppressWarnings("rawtypes")
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			sb.append(k + "=" + v + "&");
		}
		String params = sb.substring(0, sb.lastIndexOf("&"));
		String stringSignTemp = params+"&key="+ ConstantUtil.WX_API_KEY;
//		String enc = TenpayUtil.getCharacterEncoding(this.request, this.response);
		String appsign = MD5Util.MD5Encode(stringSignTemp.toString(), "utf-8").toUpperCase();
		
		return appsign;
	} 
	
}

